package cn.jiangjiesheng.bootstrap.xxl.job;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by dev@jiangjiesheng.cn on 2021/9/27  <br/>
 *
 * @author dev@jiangjiesheng.cn
 */
@EnableConfigurationProperties(XxlJobProperties.class)
@Slf4j
@ConditionalOnProperty(prefix = XxlJobProperties.PREFIX, name = "enabled", havingValue = "true")
@Configuration
public class XxlJobAutoConfiguration {


    @Bean
//    @RefreshScope
    public XxlJobSpringExecutor xxlJobExecutor(XxlJobProperties properties) throws Exception {
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(properties.getAdminAddresses());
        xxlJobSpringExecutor.setAppname(properties.getExecutor().getAppname());
        xxlJobSpringExecutor.setIp(properties.getExecutor().getIp());
        xxlJobSpringExecutor.setPort(properties.getExecutor().getPort());
        xxlJobSpringExecutor.setAccessToken(properties.getAccessToken());
        xxlJobSpringExecutor.setLogPath(properties.getExecutor().getLogpath());
        xxlJobSpringExecutor.setLogRetentionDays(properties.getExecutor().getLogretentiondays());
        xxlJobSpringExecutor.setAddress(properties.getExecutor().getAddress());
        log.info("xxljob executor inited with {}", properties);
        //xxlJobSpringExecutor.start();
        return xxlJobSpringExecutor;
    }
}
